import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../base/EnumIndex";
import { Tools } from "../common/Tools";
import { AudioManager } from "../manager/AudioManager";
import { EventManager } from "../manager/EventManager";
import { UIManager } from "../manager/UIManager";
import GameUI from "./GameUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SettingUI extends cc.Component {
    public static instance: SettingUI = null;

    @property({ type: [cc.Node], tooltip: "按钮s" })
    private switchs: cc.Node[] = [];

    @property({ type: [cc.Node], tooltip: "需要隐藏按钮" })
    private hideBtns: cc.Node[] = [];

    private storages: string[] = ["isPlayBG", "isPlayAudio", "isVib"];

    protected async onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this);

        SettingUI.instance = this;

        this.switchs.forEach((o, i) => {
            const btnOn = cc.find(`btnOn`, o);
            const btnOff = cc.find(`btnOff`, o);
            const get = Tools.getStorage(this.storages[i]);
            const value = get || get == null ? true : false;
            btnOn.active = value;
            btnOff.active = !value;
        });
    }

    protected onDestroy(): void {
        EventManager.ins.off(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this);
    }

    private openUI(data) {
        // console.log("data:", data);
        this.node.y += data?.isMain ? 100 : 0;
        this.hideBtns.forEach((o, i) => {
            o.active = data?.isMain;
        });
    }

    private change(i: number, btnOn: cc.Node, btnOff: cc.Node) {
        const key = this.storages[i];
        const get = Tools.getStorage(key) ? true : false;
        const set = !get;
        btnOn.active = set;
        btnOff.active = !set;

        switch (i) {
            case 0:
                AudioManager.setMusicStorage(set);
                break;
            case 1:
                AudioManager.setAudioStorage(set);
                break;
            case 2:
                AudioManager.setVibStorage(set);
                break;
        }
    }

    private onBtnsClicked(event: cc.Event, data: cc.Event.EventCustom) {
        const name = event.target.name;
        switch (name) {
            case "btnClose":
                {
                    UIManager.ins.close();
                }
                break;
            case "btnRestart":
                {
                    UIManager.ins.close();
                    GameUI.instance.restartGame();
                }
                break;
            case "btnBack":
                {
                    UIManager.ins.closeAll();
                    UIManager.ins.showUI("MenuUI", BUNDLE_TYPE_ENUM.GAME_PLAY);
                }
                break;
            case "btnOn":
            case "btnOff":
                {
                    for (let i = 0; i < this.switchs.length; i++) {
                        const btnOn = cc.find("btnOn", this.switchs[i]);
                        const btnOff = cc.find("btnOff", this.switchs[i]);
                        const btn = name === "btnOn" ? btnOn : btnOff;
                        if (btn === event.target) {
                            this.change(i, btnOn, btnOff);
                            break;
                        }
                    }
                }
                break;
        }
        // console.log("onReviveClicked()===>按钮:", name);
    }
}